l1 = [i for i in range(1, 100) if i % 2 == 0 and i % 5 == 0]
print(l1)  # [10, 20, 30, 40, 50, 60, 70, 80, 90]

l2 = [(i, j) for i in range(1, 5) if i % 2 == 0 for j in range(1, 5) if j % 2 == 1]
print(l2)  # [(2, 1), (2, 3), (4, 1), (4, 3)] (偶数,奇数)

# 集合推导式
l1 = [1, 2, 3, 4, 5, 3, 3, 3]
s1 = {x + 1 for x in l1 if x < 3}
print(s1)  # {2, 3}

# 字典推导式
# 交换键值
dict1 = {'A': 'a', 'B': 'b', 'C': 'c', 'D': 'd'}
dict2 = {value: key for key, value in dict1.items()}
print(dict2)



# 生成器
def fun1():
    l1=(x*1 for x in range(1,5))
    try:
        while True:
            print(l1.__next__())
    except Exception as err:
        print('错误类型',err)

# fun1()





'''
函数方法定义生成器
定义一个函数 用yield关键字
调用函数 接收函数的结果
得到的结果就是生成器
'''
def fun2():
    n=1
    while True:
        n=n+1
        yield n

def test_fun2():
    g=fun2()
    while True:
        print(g.__next__())
# test_fun2()


def fib():
    a,b=0,1
    while True:
        yield a
        a,b=b,a+b
def test_fib():
    g=fib()
    for i in range(10):
        print(g.__next__())
# test_fib()



# 向生成器中传递数据
def fun3():
    i=0
    while True:
        tem=yield i
        print(tem)
def test_fun3():
    g=fun3()
    for i in range(4):
        if(i==0):
            g.send(None)
        else:
            g.send(i)
        print('当前值为',g.__next__())

test_fun3()

